﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI.Methods
{
   public class campaignSendTest : Api_BaseExecute
   {
      private new campaignSendTestInput Input { get; set; }
      private campaignSendTestOutput Output { get; set; }
      // =============================================== constructors
      public campaignSendTest() { }
      // ---------
      public campaignSendTest( campaignSendTestInput input ) { Input = input; }
      // =============================================== Execute
      public campaignSendTestOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public campaignSendTestOutput Execute( campaignSendTestInput input )
      {
         Input = input;
         Output = new campaignSendTestOutput( Input );
         ValidationIsOK = true;
         //-------------------------
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #region ================================================================= API Calls

      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         // NOTE: send_type of string.empty does not send ANY email even though it 
         //       doesn't cause an error. A null parm in send_type DOES cause an xml-rpc exception, 
         //       so two signatures are required in IMC
         // ---------------------------------- 
         try
         {
            string[] emails = apiHelper.MakeArrayFromList<string>( Input.parms.test_emails );
            string emailString = apiHelper.MakeStringFromList( Input.parms.test_emails );
            bool rpcResult;
            if ( Input.parms.send_type != EnumValues.campaignSendTest_send_type.NotSpecified )
            {
               string send_type = Input.parms.send_type.ToString();
               rpcResult = apiXmlRpc.api.campaignSendTest( Input.parms.apikey, Input.parms.cid, emails, send_type );
            }
            else
               rpcResult = apiXmlRpc.api.campaignSendTest( Input.parms.apikey, Input.parms.cid, emails );
            // ---------------------------------- 
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            string send_type = "";
            if ( Input.parms.send_type != EnumValues.campaignSendTest_send_type.NotSpecified )
               send_type = Input.parms.send_type.ToString();

            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "cid", Input.parms.cid );
            if ( !string.IsNullOrEmpty( send_type ) )
               dict.Add( "send_type", send_type );
            for ( int i = 0; i < Input.parms.test_emails.Count; i++ )
            {
               dict.Add( string.Concat( "test_emails[", i, "]" ), Input.parms.test_emails[i] );
            }
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      #endregion
   }
}
